<?php

namespace App\Services\Strator;

use App\Models\Strator\StratorProductSpec;

class StratorProductSpecService
{

    //
    public function getStratorProductSpecs(): array{
        $stratorProductSpecs=StratorProductSpec::with(['parent'])
            ->where('pid',0)
            ->orderBy('sort','asc')
            ->get()
            ->toArray();
        foreach ($stratorProductSpecs as $key=>$productSpec){
            $stratorProductSpecs[$key]['children']=$this->getStratorProductSpecsByPid($productSpec['id']);
        }
        return $stratorProductSpecs;
    }

    //
    public function getStratorProductSpecsByPid($pid): array{
        $children=StratorProductSpec::with(['parent'])->where('pid',$pid)
            ->orderBy('sort','asc')
            ->get()->toArray();
        foreach ($children as $key=>$productSpec){
            $children[$key]['children']=$this->getStratorProductSpecsByPid($productSpec['id']);
        }
        return $children;
    }

}
